// 保存与会者信息
let participantInfo = {
  uuName: '', //参会者的名称
  uuid: '', //参会者的uuid
  role: '', //是否为主持人
  is_vmuted: '', //是否禁画
  wait_uuid: '', //等候室中参会者人的uuid
  wait_name: '' //等候室中参会人的名称
}

// 示例开始，以下几行代码即可成功入会专属云平台:
var inMeeting = false
window.onload = function () {
  getDemoConfig()
  window.zjsdk3 = new ZjSdk3()
  //重写回调
  zjsdk3.on_dev_participant_update = demo_on_dev_participant_update //参会者信息由更新的回调
  zjsdk3.on_dev_chat_message = demo_on_dev_chat_message //更新当前的聊天信息的回调
  zjsdk3.on_dev_conference_info_changed = demo_on_dev_conference_info_changed //会议信息有变化的回调
  zjsdk3.on_dev_call_succeed = demo_on_dev_call_succeed //呼叫成功时的回调
}

// 参会者信息有更新
function demo_on_dev_participant_update(pp, changed_keys) {
  // 保存到 Map 中
  participantInfo.uuName = pp.display_name
  participantInfo.uuid = pp.uuid
  participantInfo.role = pp.role
  participantInfo.is_vmuted = pp.is_vmuted
  if (pp.service_type == 'waiting_room') { //等候大厅中参会人的uuid和display_name
    participantInfo.wait_uuid = pp.uuid
    participantInfo.wait_name = pp.display_name
  }
  document.querySelector('#overlay_text_participant_name').innerHTML = participantInfo.uuName
  document.querySelector('#SetpointText').innerHTML = pp.focus_stat == 2 ? '焦点' : '不是焦点'
}

// 更新当前的聊天信息
function demo_on_dev_chat_message(from_uuid, message, nickname, is_private) {
  document.querySelector('#remote_chat_msg').innerHTML = message
}

// 会议信息变化
function demo_on_dev_conference_info_changed(cinfo) {
  document.getElementById("lock_unlock_mr").innerText = cinfo['locked'] ? "已锁定" : "未锁定"
  document.getElementById("mute_all_guest_tip").innerText = cinfo['guests_muted'] ? "已被静音" : "未被静音"
  document.getElementById("current_record_status").innerText = cinfo['record_status'] ? "正在录制" : "未录制"
  document.getElementById("current_live_status").innerText = cinfo['live_status'] ? "正在直播" : "未直播"
}

// 呼叫成功时的回调
function demo_on_dev_call_succeed() {
  //回显本地视频
  zjsdk3.local_dev_show_myvideo(['local_video'], true)
  //显示远端视频
  zjsdk3.show_video_by_orders(['farend_video', 'farend_video2', 'farend_video3', 'farend_video4', 'farend_video5'])
}

// 加入会议
window.start_call = async function () {
  if (!inMeeting) {
    inMeeting = true
    // 将用户输入的服务器地址、会议号、入会密码、昵称存入配置文件中
    setDemoConfig()
    await zjsdk3.make_call_now(demo_config.server.ip, demo_config.server.port, demo_config.meeting.cuid, demo_config.meeting.joinpwd, demo_config.meeting.nick_name, demo_config.meeting.account)
  }
}

// 离开会议室
window.disconnect = async function () {
  await zjsdk3.disconnect_now()
  window.inMeeting = false
}

//结束会议
window.disconnect_all = function () {
  zjsdk3.host_end_conference()
}

//设置为焦点
window.Setpoint = function (is_on) {
  zjsdk3.host_spotlight_on(participantInfo.uuid, is_on)
}

//接收媒体流的统计信息
window.get_media_statistics = async function () {
  var rtpArr = await zjsdk3.get_inbound_statistics()
  document.querySelector('#media_statistics_result').innerHTML = JSON.stringify(rtpArr)
}

//清空内容
window.clear_media_statistics_result = function () {
  document.querySelector('#media_statistics_result').innerHTML = ''
}

// 发送媒体流的统计信息
window.set_media_statistics = async function () {
  var set_rtpArr = await zjsdk3.get_outbound_statistics()
  document.querySelector('#set_media_statistics_result').innerHTML = JSON.stringify(set_rtpArr)
}

//清空内容(发送)
window.set_clear_media_statistics_result = function () {
  document.querySelector('#set_media_statistics_result').innerHTML = ''
}

//群发文字聊天信息
window.send_chat_message = function () {
  const message = document.getElementById("chatMsg").value
  if (message.trim() === "") {
    alert("请输入想要群发的聊天信息！")
    return
  }
  zjsdk3.buffet_send_chat_message(message, participantInfo.uuid)
}

// 锁定会议
window.set_conference_lock = function (is_lock) {
  zjsdk3.host_lock_conference(is_lock)
}

//开启会议直播
window.switch_live = function (is_open) {
  zjsdk3.host_open_live(is_open)
}

//开启会议录制
window.switch_record = function (is_open) {
  zjsdk3.host_open_record(is_open)
}

//设置观众静音
window.set_mute_all_guests = function (is_mute) {
  zjsdk3.host_mute_all_guests(is_mute)
}

// 获取参会者列表（初始化数据）
function initData(Id) {
  var attendeeList = zjsdk3.get_all_participants()
  let select = document.getElementById(Id)
  select.options.length = 0
  attendeeList.forEach(item => {
    select.options.add(new Option(item.display_name, item.uuid))
  })
}

// 获取等候大厅参会者的列表
function waitData(Id) {
  var waitList = zjsdk3.get_all_participants()
  let select = document.getElementById(Id)
  select.options.length = 0
  waitList.forEach(item => {
    if (item.service_type == 'waiting_room') {
      select.options.add(new Option(item.display_name, item.uuid))
    }
  })
}

// 当前选中的参会人
function attendeeData(id, name, uuid) {
  var select = document.getElementById(id)
  var selectValue = document.getElementById(id).value
  var text = select.options[select.selectedIndex].text
  document.querySelector(name).innerHTML = text
  document.querySelector(uuid).innerHTML = selectValue
}

//外呼参会者
window.dial_out = function () {
  let destination = document.getElementById("destination_addr").value //当前外呼的地址
  let prototype = document.getElementById("dial_out_prototype_select").value //当前外呼的协议
  if (destination === "") {
    alert("请输入被呼地址！")
    return
  }
  zjsdk3.host_dial_out(destination, prototype)
}

// 将等待中的参会者放进会议室
window.init_unlock_participant_select = function () {
  // 初始化数据
  waitData('unlock_participant_select')
  document.querySelector('#unlock_participant_name').innerHTML = participantInfo.wait_name
  document.querySelector('#unlock_participant_uuid').innerHTML = participantInfo.wait_uuid
}
// 是否放入会议室
window.unlock_participant = function () {
  var waitUUid = document.getElementById("unlock_participant_select").value
  zjsdk3.host_unlock_participant(waitUUid)
}

// 设置/取消某指定参会者静音
window.mute_action_click = function () {
  //获取参会者列表
  initData('mute_select')
  // 获取当前选中的参会者
  attendeeData('mute_select', '#mute_participant_name', '#mute_participant_uuid')
}
// 当前的参会者（选中了谁）
window.mute_select_change = function () {
  attendeeData('mute_select', '#mute_participant_name', '#mute_participant_uuid')
}
// 将参会者静音
window.set_participant_mute = function () {
  var Mute = document.getElementById("mute_action").value
  var MuteId = document.getElementById("mute_select").value
  let mutedFlag = null
  if (Mute == '') {
    alert('请选择是否静音或取消静音')
    return false
  }
  if (Mute == 0) {
    mutedFlag = true
  } else {
    mutedFlag = false
  }
  zjsdk3.host_mute_participant_audio(MuteId, mutedFlag)
}

//设置参会者身份
window.init_set_role_select = function () {
  //获取参会者列表
  initData('set_role_select')
  // 获取当前选中的参会者
  attendeeData('set_role_select', '#set_role_participant_name', '#set_role_participant_uuid')
}
// 当前的参会者（选中了谁）
window.set_role_select_change = function () {
  attendeeData('set_role_select', '#set_role_participant_name', '#set_role_participant_uuid')
}
// 确认转换
window.toggle_role = function () {
  var RoleUUId = document.getElementById("set_role_select").value
  var chairFlag = null
  if (participantInfo.role == 'chair') {
    chairFlag = false
  } else {
    chairFlag = true
  }
  zjsdk3.host_set_participant_host(RoleUUId, chairFlag)
}

// 修改参会人员名称
window.init_overlayText_select = function () {
  //获取参会者列表
  initData('overlay_text_select')
  // 获取当前的参会者
  attendeeData('overlay_text_select', '#overlay_text_participant_name', '#overlay_text_participant_uuid')
}
// 当前的参会者（选中了谁）
window.overlay_text_select_change = function () {
  attendeeData('overlay_text_select', '#overlay_text_participant_name', '#overlay_text_participant_uuid')
}
// 确认修改
window.overlay_text_update = function () {
  var input = document.getElementById("new_name").value
  var attendeeUUid = document.querySelector('#overlay_text_select').value
  zjsdk3.host_set_participant_name(attendeeUUid, input)
}

// 禁画/取消禁画
window.video_mute_action_change = function () {
  //获取参会者列表
  initData('video_mute_select')
  // 获取当前的参会者
  attendeeData('video_mute_select', '#video_mute_participant_name', '#video_mute_participant_uuid')
}
// 当前的参会者（选中了谁）
window.video_mute_select_change = function () {
  attendeeData('video_mute_select', '#video_mute_participant_name', '#video_mute_participant_uuid')
}
// 将参会者禁画
window.set_participant_video_mute = function () {
  var ProhibitObjUUid = document.querySelector('#video_mute_select').value
  var isVmuted = document.querySelector('#video_mute_action').value
  let paintingFlag = null
  if (isVmuted == 0) {
    paintingFlag = true
  } else {
    paintingFlag = false
  }
  console.log(ProhibitObjUUid, paintingFlag)
  zjsdk3.host_mute_participant_video(ProhibitObjUUid, paintingFlag)
}

//将指定参会者移出会议
window.init_disconnet_participant_select = function () {
  //获取参会者列表
  initData('disconnet_participant_select')
}
window.disconnectParticipant = function () {
  var shiftoutUUid = document.getElementById("disconnet_participant_select").value
  zjsdk3.host_disconnect_participant(shiftoutUUid)
}